
package com.lab.utils;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.DatePicker;
import android.widget.Toast;

import com.cuitrip.app.MainApplication;
import com.cuitrip.service.R;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class MessageUtils {

    private static Toast mToast = null;

    public static boolean isAfter5() {
        return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT);
    }

    public static void showDateCheck(FragmentActivity context, final DateCheckListener dateCheckListener) {
        AlertDialog.Builder builder = MessageUtils.createHoloBuilder(context);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+800"));
//        if (isAfter5())
        {
            final DatePicker datePicker = new DatePicker(context);
            int year = calendar.get(Calendar.YEAR);
            int monthOfYear = calendar.get(Calendar.MONTH);
            int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
            datePicker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {

                public void onDateChanged(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                    LogHelper.e("omg", "onDateChanged");
                }

            });
            builder.setView(datePicker);
            builder.setPositiveButton(R.string.operation_yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    if (dateCheckListener != null) {
                        dateCheckListener.onDataSelect(datePicker.getYear() + "-" +
                                fillToTwo(datePicker.getMonth() + 1)
                                + "-" + fillToTwo(datePicker.getDayOfMonth()));
                    }
                    dialog.dismiss();
                }
            });
            AlertDialog dialog = builder.show();
            dialog.show();
        }
//        else
//        {
//            DatePickerDialog dpd = DatePickerDialog.newInstance(
//                    new DatePickerDialog.OnDateSetListener() {
//                        @Override
//                        public void onDateSet(DatePickerDialog datePickerDialog, int i, int i1, int i2) {
//                            if (dateCheckListener != null) {
//                                dateCheckListener.onDataSelect(i + "-" +
//                                        fillToTwo(i1+1)
//                                        + "-" + fillToTwo(i2));
//                                datePickerDialog.dismiss();
//                            }
//                        }
//                    },
//                    calendar.get(Calendar.YEAR),
//                    calendar.get(Calendar.MONTH),
//                    calendar.get(Calendar.DAY_OF_MONTH)
//            );
//            dpd.dismissOnPause(true);
//            dpd.setAccentColor(PlatformUtil.getInstance().getColor(R.color.ct_main_color_light));
//            dpd.show(context.getFragmentManager(), "Datepickerdialog");
//        }
    }

    public static String fillToTwo(int value) {
        DecimalFormat df = new DecimalFormat("00");
        return df.format(value);
    }

    public static void showToast(int resId) {
        showToast(null, MainApplication.getInstance().getString(resId));
    }

    public static void showToast(String tips) {
        showToast(null, (null == tips || TextUtils.isEmpty(tips.trim())) ? "unknow" : tips);
    }

    public static void showToast(Context context, String toastString) {
        if (context == null) {
            context = MainApplication.getInstance();
        }
        if (mToast == null) {
            mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
        }
        mToast.setText(toastString);
        mToast.show();
    }

    public static void showToastCenter(String toastString) {
        if (mToast == null) {
            mToast = Toast.makeText(MainApplication.getInstance(), "", Toast.LENGTH_SHORT);
        }
        mToast.setText(toastString);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    public static ProgressDialog getProgressDialog(Context context) {
        ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setIndeterminate(true);
        progressDialog.setCancelable(true);
        progressDialog.setMessage("");
        progressDialog.setTitle("");
        return progressDialog;
    }

    public static ProgressDialog getProgressDialog(Context context, int stringId) {
        ProgressDialog progressDialog = getProgressDialog(context);
        progressDialog.setMessage(context.getString(stringId));
        return progressDialog;
    }

    public static ProgressDialog getNoCancelProgressDialog(Context context, String msg) {
        ProgressDialog progressDialog = getProgressDialog(context);
        progressDialog.setMessage(msg);
        progressDialog.setCancelable(false);
        return progressDialog;
    }

    public static AlertDialog.Builder createHoloBuilder(Context context) {
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(context, R.style.cuiAppDialog);
        return builder;
    }

    public static AlertDialog.Builder createBuilder(Context context) {
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(context);
        return builder;
    }

    public interface DateCheckListener {
        void onDataSelect(String s);
    }

    public interface setMessageListener {
        void setMessage(String s);
    }
}
